package com.guldan.jaina.provider;

import com.guldan.core.constants.CommonConstant;
import com.guldan.core.dubbo.client.ResponseDTO;
import com.guldan.core.exception.BusinessException;
import com.guldan.core.redis.CacheService;
import com.guldan.core.search.Pagination;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.citizeninfo.CitizenInfoDTO;
import com.guldan.jaina.dto.citizeninfo.CitizenInfoSearchDTO;
import com.guldan.jaina.dto.datastat.datav.DatavDTO;
import com.guldan.jaina.manager.CitizenInfoManager;
import com.guldan.jaina.manager.ReportManager;
import com.guldan.jaina.manager.TestManager;
import com.guldan.jaina.task.CreateDatavDataTask;
import com.guldan.jaina.task.SyncRedisDataHandler;
import com.guldan.jaina.utils.validating.CheckIdCard;
import com.guldan.mapper.db.entity.CitizenInfo;
import com.guldan.mapper.db.entity.CitizenInfoExample;
import com.guldan.mapper.db.mapper.CitizenInfoMapperExt;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Silas.
 * Date: 2016/12/11
 * Time: 12:32
 */
@Component
public class TestProviderImpl implements ITestProvider {

    public Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private TestManager testManager;

    @Override
    public ResponseDTO<String> getCustomException() {

        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            testManager.testExecption();
            System.gc();
        } catch (BusinessException ex) {

            logger.info("捕获异常");
            logger.info(ex.getMessage());
            responseDTO.setException(ex);
            responseDTO.setStatus(false);
        }

        logger.info("捕获异常" + JSONObject.fromObject(responseDTO).toString());

        return responseDTO;
    }

    private String g() {
        throw new BusinessException("这是自定义异常返回的文案内容");
    }

    public ResponseDTO<String> timeOutTest() {
        int tick = 3600;
        while (tick > 0) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            tick--;
        }
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        responseDTO.setData("结束");
        return responseDTO;
    }

    @Autowired
    private SyncRedisDataHandler syncRedisDataHandler;

    @Override
    public ResponseDTO<Boolean> redisData() {
        long begin = System.currentTimeMillis();
        try {

            syncRedisDataHandler.handler();

        } catch (Exception ex) {
            logger.error(ex.getMessage());
            ex.printStackTrace();
        } finally {
            logger.info("syncRedisDataHandler.handler() cost=" + (System.currentTimeMillis() - begin));
            ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
            responseDTO.setData(true);
            return responseDTO;
        }
    }

    @Override
    public ResponseDTO<String> bigDataObject(String summaryId) {

        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        responseDTO.setData(testManager.bigObject(summaryId));
        return responseDTO;
    }

    @Autowired
    private ReportManager reportManager;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private CreateDatavDataTask createDatavDataTask;

    @Override
    public ResponseDTO<String> createDataV() {


        try {
            createDatavDataTask.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*DatavDTO datavDTO = reportManager.getDatavDTO("630000", false);

        try {
            logger.info(JSONObject.fromObject(datavDTO).toString());
            cacheService.putCache(String.format(CommonConstant.KEY_DATAV_AREA_DATA, datavDTO.getAreaId()), datavDTO);
        } catch (Exception ex) {
            logger.info("put datav to redis fail, datavDTO.getAreaId() %s", datavDTO.getAreaId());
        }
        */
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        responseDTO.setData("true");
        return responseDTO;
    }


    @Autowired
    private CitizenInfoMapperExt citizenInfoMapperExt;

    @Autowired
    private CitizenInfoManager citizenInfoManager;

    @Override
    public ResponseDTO<String> checkCitizen() {


        /*int index = 1;
        int size = 1;
        boolean flag = true;
        while (flag) {
            CitizenInfoSearchDTO searchDTO = new CitizenInfoSearchDTO();
            searchDTO.setPerPage(5000);
            searchDTO.setCurrentPage(index);
//            Pagination<CitizenInfoDTO> list ]= citizenInfoManager.list(searchDTO);
            Pagination<CitizenInfoDTO> list2 = new Pagination<CitizenInfoDTO>(searchDTO.getCurrentPage(), searchDTO.getPerPage());

            CitizenInfoExample example = new CitizenInfoExample();
            CitizenInfoExample.Criteria criteria = example.createCriteria();
            criteria.andIsDeleteEqualTo(Boolean.FALSE);
            example.setOrderByClause("citizen_info_id asc");

            int total = citizenInfoMapperExt.countByExample(example);
            list2.setTotalCount(total);

            if (total > 0) {
                List<CitizenInfo> list1 = citizenInfoMapperExt.selectByExampleWithPaging(example, list2.getOffset(), list2.getPageSize());

                if (list1 != null && list1.size() > 0) {
                   *//* for (CitizenInfo dto : list1) {

                        cc(dto);

                        logger.info("\nsize：" + size + "\n");

                        size++;

                    }*//*

                    Thread thread = new Thread(new CheckThread(list1, citizenInfoManager));
                    thread.start();
                }

            } else {
                //结束
                flag = false;
            }
            index++;
//            flag = false;

        }*/
        return null;
    }

    @Override
    public ResponseDTO<Boolean> createMysqlData(String divisionCode) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        responseDTO.setData(reportManager.statDatavDataToMysql(divisionCode));
        return responseDTO;
    }


    class CheckThread implements Runnable {

        private List<CitizenInfo> list;

        private CitizenInfoManager citizenInfoManager;


        public CheckThread(List<CitizenInfo> list, CitizenInfoManager citizenInfoManager) {
            this.list = list;
            this.citizenInfoManager = citizenInfoManager;
        }

        @Override
        public void run() {

            for (CitizenInfo entity : list) {
                citizenInfoManager.checkCitizen(entity);
            }

        }
    }

}
